//
//  MPEditViewController.m
//  SearchPeople
//
//  Created by YoloMao on 16/5/12.
//
//

#import "MPEditViewController.h"
#import "RFSegmentView.h"

@interface MPEditViewController ()

@property (weak, nonatomic) IBOutlet UITextField *editTextField;

@end

@implementation MPEditViewController

#pragma mark - UIViewController Life Cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"确定"
                                                            style:UIBarButtonItemStylePlain
                                                           target:self
                                                           action:@selector(confirmButtonClick)];
    self.navigationItem.rightBarButtonItem = item;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.editTextField.text = self.text;
    if (self.userInfoType == MPUserInfoTypeGender) [self showSegmentControl];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Action

- (void)confirmButtonClick
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(updateUserInfoWithString:withType:)])
    {
        if (self.userInfoType == MPUserInfoTypeGender)
        {
            [self.delegate updateUserInfoWithString:self.gender withType:self.userInfoType];
        }else
        {
            [self.delegate updateUserInfoWithString:self.editTextField.text withType:self.userInfoType];
        }
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - Private method

- (void)showSegmentControl
{
    self.editTextField.hidden = YES;
    RFSegmentView *segmentView = [[RFSegmentView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) items:@[@"男",@"女"]];
    
    segmentView.tintColor       = CustomTintColor;
    segmentView.selectedIndex   = self.gender.length > 0 ? ([self.gender isEqualToString:@"男"] ? 0 : 1) : 0;
    segmentView.itemHeight      = 30.f;
    
    __weak typeof(self) weakSelf = self;
    segmentView.handlder = ^ (RFSegmentView * __nullable view, NSInteger selectedIndex) {
        weakSelf.gender = selectedIndex == 0 ? @"男" : @"女";
    };
    [self.view addSubview:segmentView];
    
    
    [segmentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@40);
        make.left.equalTo(weakSelf.view.mas_left).offset(0);
        make.right.equalTo(weakSelf.view.mas_right).offset(0);
        make.top.equalTo(weakSelf.view.mas_top).offset(20);
    }];
}

#pragma mark - Getter && Setter

- (NSString *)gender
{
    if (!_gender)
    {
        _gender = @"男";
    }
    return _gender;
}
@end
